home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C011.ZIP / BCOMBINE.C next >
Text File  |  1990-01-19  |  3KB  |  127 lines

  1. /********************************************************************
  2.  * C Users Group (U.K) C Source Code Library File CUGLIB.011        *
  3.  * Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd.         *
  4.  * Edgbaston, Birmingham B15 2QN ENGLAND                *
  5.  ********************************************************************
  6.  * File name: bcombine.c
  7.  * Program name: bcombine
  8.  * Source of file: written by Martin Houston
  9.  * Purpose: 
  10.  *
  11.  * bcombine: a program that undoes the affect of bsplit
  12.  *
  13.  * bcombine [name[file]]
  14.  *
  15.  * name is the prefix used for the files read in.
  16.  * file is the destination for the re-combined file.
  17.  * Standard out is used if this is not specified.
  18.  * The first file read will be {name}aa, the second {name}ab and so on.
  19.  *
  20.  * Changes: <who what when & why major changes have been made>      
  21.  ********************************************************************/
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #define BS 20480
  25. #define SS 40
  26.  
  27. extern long atol();
  28.  
  29. /*
  30.  * Get the next name in the series.
  31.  */
  32. static char *namegen(seed)
  33. char *seed;
  34. {
  35.     static int count = 0;
  36.     static char1 = 'a';
  37.     static char2 = 'a';
  38.     char result[SS];
  39.  
  40.     if(strlen(seed) > (SS - 2))
  41.         seed[SS - 2] = 0; /* truncate so result will fit */
  42.  
  43.     sprintf(result,"%s%c%c", seed, char1, char2);
  44.     if(++char2 > 'z')
  45.     {
  46.         char2 = 'a';
  47.         if(++char1 > 'z')
  48.         {
  49.         fprintf(stderr,"bcombine has run out of file names!\n");
  50.         exit(0);
  51.         }
  52.     }
  53.     return result;
  54. }
  55.  
  56. main(argc, argv)
  57. int argc;
  58. char **argv;
  59. {
  60.  
  61.     int fdesc = dup(1);     /* std out */
  62.     char *filename = "x";    /* default of no name specified */
  63.  
  64.     
  65.     if(argc > 1)
  66.     {
  67.         filename = argv[1];
  68.         argv++;    /* move count on */
  69.         argc--;
  70.         if(argc > 1)
  71.         {
  72.         if(argv[1][0] != '-')
  73.         {
  74.             fdesc = open(argv[1], O_WRONLY | O_CREAT, 0666);
  75.             if(fdesc < 0)
  76.             {
  77.             fprintf(stderr,"bcombine cannot open %s\n", argv[1]);
  78.             exit(0);
  79.             }
  80.         }
  81.         }
  82.     }
  83.  
  84.     combineit(fdesc, filename);
  85. }
  86.  
  87. combineit(output, seed)
  88. int output;
  89. char *seed;
  90. {
  91.     static char buf[BS];    /* io buffer */
  92.     char *inname;
  93.     int rcnt, wcnt;
  94.     int infd;
  95.  
  96.     inname = namegen(seed);
  97.     if((infd = open(inname, O_RDONLY)) < 0)
  98.     {
  99.         close(output); /* end of the line */
  100.         exit(0);
  101.     }
  102.     while((rcnt = read(infd, buf, BS)) > 0)
  103.     {
  104.         wcnt = write(output, buf, rcnt);
  105.         if(rcnt < 0)
  106.         {
  107.         /* don't stop processing */
  108.         fprintf(stderr, "bcombine: read error\n");
  109.         }
  110.         if(wcnt < rcnt)
  111.         {
  112.         fprintf(stderr, "bcombine: write error\n");
  113.         }
  114.         if(rcnt < BS)
  115.         {
  116.         close(infd);
  117.             inname = namegen(seed);
  118.             if((infd = open(inname, O_RDONLY)) < 0)
  119.             {
  120.             close(output);
  121.                 exit(0);
  122.             }
  123.             }
  124.     }
  125. }
  126.  
  127.